﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace HFController
{
    public class TeamworkController:ControllerBase
    {
        //单利对象
        private static TeamworkController _jobsControleer;

        //实例化单列对象
        public static TeamworkController getController()
        {
            if (_jobsControleer == null)
            {
                _jobsControleer = new TeamworkController();
            }
            return _jobsControleer;
        }

        /// <summary>
        /// 数据库访问层
        /// </summary>
        private hf.DAL.TeamworkDAL TeamworkDAL = new hf.DAL.TeamworkDAL();

        /// <summary>
        /// 得到指定数量的信息列表
        /// </summary>
        /// <param name="Count"></param>
        /// <returns></returns>
        public List<hf.Model.TeamworkModel> getTeamworkList(int Count)
        {
            List<hf.Model.TeamworkModel> list = new List<hf.Model.TeamworkModel>();

            //没有显示记录
            int PageSize = Count;

            //记录总条数
            int total;
            //筛选条件
            string strSql = "";

            //获得列表
            list = TeamworkDAL.GetTeamworkList(PageSize, 1, strSql, out total);

            this.BindParameter(list);

            return list;
        }

        /// <summary>
        /// 内部方法，绑定
        /// </summary>
        /// <param name="list"></param>
        private void BindParameter(List<hf.Model.TeamworkModel> list)
        {
            BagePages bp = new BagePages();
            foreach (hf.Model.TeamworkModel ask in list)
            {
                ask.CityName = bp.getCityName(ask.CityID.ToString());
                ask.ProvinceName = bp.getProvinceName(ask.ProvinceID.ToString());
                hf.Model.Lawyer lawyer = LawyerController.getController().getLawyerModel(ask.LawyerID);
                if (lawyer != null)
                {
                    ask.LawyerName = LawyerController.getController().getLawyerModel(ask.LawyerID).RealName;
                }
                else {
                    ask.LawyerName = "";
                }
            }
        }

        /// <summary>
        /// 得到列表
        /// </summary>
        /// <param name="PagerStr"></param>
        /// <returns></returns>
        public List<hf.Model.TeamworkModel> getTeamworkList(out string PagerStr)
        {
            List<hf.Model.TeamworkModel> list = new List<hf.Model.TeamworkModel>();

            //没有显示记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }
            //记录总条数
            int total;
            //筛选条件
            string strSql = "";

            //获得列表
            list = TeamworkDAL.GetTeamworkList(PageSize, PageNumber, strSql, out total);

            //分页
            PagerStr = LLL.Pager.TrunPageHtmlCode(PageSize, PageNumber, total, "");

            return list;
        }

        /// <summary>
        /// 根据ID得到一条律师合作
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public hf.Model.TeamworkModel getTeamworkModel(int ID)
        {
            hf.Model.TeamworkModel model = new hf.Model.TeamworkModel();
            int total;
            List<hf.Model.TeamworkModel> list = TeamworkDAL.GetTeamworkList(1, 1, "ID=" + ID.ToString(), out total);
            if (list.Count > 0)
            {
                model = list[0];
            }
            else
            {
                model = null;
            }
            return model;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ID"></param>
        public void doDelegeTeamwork(int ID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();

            try
            {
                TeamworkDAL.Delete(ID);

                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='Teamlist.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="IDS"></param>
        public void doDelegeTeamwork(string[] IDS)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            try
            {
                for (int i = 0; i < IDS.Length; i++)
                {
                    TeamworkDAL.Delete(int.Parse(IDS[i].Trim()));
                }
                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='Teamlist.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }
    }
}
